package rexsee.multimedia;

import java.io.File;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.utilities.Utilities;
import android.content.Context;
import android.media.MediaRecorder;

public class RexseeAudioCapture implements JavascriptInterface {

	public static final String INTERFACE_NAME = "AudioCapture";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeAudioCapture(childBrowser);
	}

	public static final String EVENT_ONAUDIOCAPTUREINFO = "onAudioCaptureInfo";

	public final Context mContext;
	private final Browser mBrowser;
	private final MediaCaptureArguments mCaptureArguments;
	private MediaRecorder recorder = null;

	private final MediaRecorder.OnErrorListener mErrorListener = new MediaRecorder.OnErrorListener() {
		@Override
		public void onError(MediaRecorder mr, int what, int extra) {
			mBrowser.exception(getInterfaceName(), "MEDIA_RECORDER_ERROR_UNKNOWN");
		}
	};
	private final MediaRecorder.OnInfoListener mInfoListener = new MediaRecorder.OnInfoListener() {
		@Override
		public void onInfo(MediaRecorder mr, int what, int extra) {
			String message;
			switch (what) {
				case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED :
					message = "MEDIA_RECORDER_INFO_MAX_DURATION_REACHED";
					break;
				case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED :
					message = "MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED";
					break;
				default :
					//message = "MEDIA_RECORDER_INFO_UNKNOWN";
					return;
			}
			stop();
			mBrowser.eventList.run(EVENT_ONAUDIOCAPTUREINFO, new String[]{message});
		}
	};

	public RexseeAudioCapture(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
		mCaptureArguments = new MediaCaptureArguments();
		mBrowser.eventList.add(EVENT_ONAUDIOCAPTUREINFO);
	}

	//JavaScript Interface

	public boolean start(String path) {
		File target = Utilities.prepareWriteFile(path);
		if (target == null) {
			mBrowser.exception(getInterfaceName(), "Invalid target file path.");
			return false;
		}
		stop();
		try {
			recorder = mCaptureArguments.getRecorder(null, null, mErrorListener, mInfoListener);
			recorder.setOutputFile(target.getAbsolutePath());
			recorder.prepare();
			recorder.start();
			return true;
		} catch (Exception e) {
			if (mBrowser != null) mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public boolean stop() {
		if (recorder != null) {
			try {
				recorder.stop();
				recorder.release();
			} catch (Exception e) {
			}
			recorder = null;
		}
		return true;
	}
	public String getArguments() {
		return mCaptureArguments.toString(false);
	}
	public void setArguments(String argu) {
		mCaptureArguments.parseArguments(argu);
	}

}
